package com.yms.yms.service.impl;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yms.yms.common.BaseResponse;
import com.yms.yms.dao.entity.YmsRole;
import com.yms.yms.dao.entity.YmsUser;
import com.yms.yms.dao.entity.YmsUserRole;
import com.yms.yms.dao.mapper.YmsRoleMapper;
import com.yms.yms.dao.mapper.YmsUserMapper;
import com.yms.yms.dao.mapper.YmsUserRoleMapper;
import com.yms.yms.service.AuthService;
import com.yms.yms.utils.RandomPwd;
import com.yms.yms.utils.StatusCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class AuthServiceImpl implements AuthService {
    @Autowired
    YmsUserMapper ymsUserMapper;
    @Autowired
    YmsRoleMapper ymsRoleMapper;
    @Autowired
    YmsUserRoleMapper ymsUserRoleMapper;

    @Override
    public BaseResponse register(String username, String password, String email, String realName) {
        // Todo 完善注册逻辑:参数检测，错误提示，密码加密
        YmsUser ymsUser = new YmsUser(username, password, email, realName);
        ymsUserMapper.insert(ymsUser);
        return new BaseResponse(StatusCodeEnum.OK, null);
    }

    @Override
    public BaseResponse login(String username, String password) {
        // Todo 完善登录逻辑：参数校验，错误提示，密码加密
        QueryWrapper<YmsUser> wrapper = new QueryWrapper<YmsUser>();
        wrapper.eq("username", username);
        YmsUser user = ymsUserMapper.selectOne(wrapper);
        if (user != null){
            if (password.equals(user.getPassword())){
                StpUtil.login(user.getId());
                SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
                return new BaseResponse(StatusCodeEnum.OK, tokenInfo);
            }else {
                return new BaseResponse(StatusCodeEnum.FAILED, null);
            }
        } else {
            return new BaseResponse(StatusCodeEnum.FAILED, null);
        }
    }

    @Override
    public BaseResponse testLogin() {
        return new BaseResponse(StatusCodeEnum.OK, null);
    }

    @Override
    public BaseResponse logout() {
        StpUtil.logout();
        return new BaseResponse(StatusCodeEnum.OK, null);
    }

    /**
     * 用于项目第一次启动创建第一个管理员用户
     * @return
     */
    @Override
    public BaseResponse generateRoot() {
        String username = "root";
        String password = RandomPwd.getRandomPwd();
        String email = "root@root.com";
        String realName = "管理员";
        log.info("root user password:{}", password);
        YmsUser root = new YmsUser(username, password, email, realName);
        YmsRole role = new YmsRole();
        role.setRoleName("admin");
        ymsRoleMapper.insert(role);
        ymsUserMapper.insert(root);
        YmsUserRole userRole = new YmsUserRole();
        userRole.setRid(role.getId());
        userRole.setUid(root.getId());
        ymsUserRoleMapper.insert(userRole);
        log.debug(root.toString());
        log.debug(role.toString());
        log.debug(userRole.toString());
        return new BaseResponse(StatusCodeEnum.OK, root);
    }
}
